<template>
  <div class="com_container">
    <div class="com_chart" ref="ref_stock"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      echartInstance: null,
      allData: [],
      titleSize: 0,
      currentIndex: 0,
      timerId: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      console.log('主题改变了')
      console.log(this.theme)

      this.echartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.upData() // 更新图表的展示
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    async initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.ref_stock, this.theme)

      const initOption = {
        title: {
          text: '▎库存与销量分析',
          left: 20,
          top: 20
        },
        series: [
          // hoverAnimation: false,
          { type: 'pie' }
        ]
      }
      this.echartInstance.setOption(initOption)
      this.echartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.echartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      const { data: res } = await this.$http.get('stock')
      console.log(res)
      this.allData = res

      this.upData()
      this.startInterval()
    },
    upData() {
      // 五个圆心位置
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      // 处理数据
      // 开始时展示前五个
      const startIndex = this.currentIndex * 5
      const endIndex = (this.currentIndex + 1) * 5

      const startData = this.allData.slice(startIndex, endIndex)
      const seriesArr = startData.map((item, index) => {
        return {
          type: 'pie',
          hoverAnimation: false,

          center: centerArr[index],
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  { offset: 0, color: colorArr[index][0] },
                  { offset: 1, color: colorArr[index][1] }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ],
          labelLine: {
            show: false
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          }
        }
      })

      const dataOption = {
        series: seriesArr
      }
      this.echartInstance.setOption(dataOption)
    },
    screenAdapter() {
      this.titleSize = (this.$refs.ref_stock.offsetWidth / 100) * 3.6
      const innerRadius = this.titleSize * 2.8
      const outterRadius = innerRadius * 1.15
      const screenOption = {
        title: {
          textStyle: {
            fontSize: this.titleSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: this.titleSize / 2
            }
          }
        ]
      }
      this.echartInstance.setOption(screenOption)
      this.echartInstance.resize()
    },
    // 定时器
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.upData()
      }, 3000)
    }
  }
}
</script>

<style lang="scss" scoped></style>
